﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.QuanLyKho;

namespace QuanLyKho.PhongLenh
{
    public partial class Lss_DuyetPhieuYeuCau : UserControl
    {
        public Lss_DuyetPhieuYeuCau()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        private void LoadData()
        {
            gridPhieuYeuCau.DataSource = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhieuYeuCau.[PhongBan],PhieuYeuCau.[ID],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhongMau_SanPham.[TuNgay],PhongMau_SanPham.[DenNgay],PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
                "WHERE PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_ChoXuLy)+" OR PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_HuyCapNhatKho));
            gridDaXuLy.DataSource = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhieuYeuCau.[PhongBan],PhieuYeuCau.[ID],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
                "WHERE PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu));
        }

        public void SetPropertiesGrid()
        {
            gvPhieuYeuCau.BestFitColumns();
            gvPhieuYeuCau.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvPhieuYeuCau.Appearance.SelectedRow.BackColor = Color.Empty;
            gvPhieuYeuCau.Appearance.SelectedRow.Options.UseBackColor = true;
            gvPhieuYeuCau.OptionsView.ShowGroupPanel = false;
            gvPhieuYeuCau.OptionsView.ColumnAutoWidth = false;
            gvPhieuYeuCau.OptionsView.ShowFooter = true;
            gvPhieuYeuCau.OptionsFind.AlwaysVisible = true;
            gvPhieuYeuCau.Columns["ID"].Visible = false;
            for (int i = 3; i < gvPhieuYeuCau.Columns.Count; i++)
            {
                gvPhieuYeuCau.Columns[i].OptionsColumn.AllowEdit = false;
            }

            gvDaXuLy.Columns["ID"].Visible = false;
        }

        private void gvPhieuYeuCau_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "PhongBan")
            {
                e.DisplayText = Common.HienThiPhongBan(Convert.ToInt32(e.Value));
            }
        }

        private void gvPhieuYeuCau_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhieuYeuCau.FocusedRowHandle >= 0)
            {
                PhieuYeuCau_ChiTiet.MaID = gvPhieuYeuCau.Columns["ID"].View.GetRowCellDisplayText(gvPhieuYeuCau.FocusedRowHandle, "ID");
                var lss = new PhieuYeuCau_ChiTiet();
                lss.ShowDialog();
                LoadData();
                PhieuYeuCau_ChiTiet.MaID = "0";
            }
        }

        private void gvDaXuLy_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "PhongBan")
            {
                e.DisplayText = Common.HienThiPhongBan(Convert.ToInt32(e.Value));
            }
        }

        private void gvDaXuLy_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhieuYeuCau.FocusedRowHandle >= 0)
            {
                PhieuYeuCau_ChiTiet.MaID = gvPhieuYeuCau.Columns["ID"].View.GetRowCellDisplayText(gvPhieuYeuCau.FocusedRowHandle, "ID");
                var lss = new PhieuYeuCau_ChiTiet();
                lss.ShowDialog();
                LoadData();
                PhieuYeuCau_ChiTiet.MaID = "0";
            }
        }

        private void btnCapNhatKho_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhieuYeuCau.DataSource;
            string message = string.Empty;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn duyệt phiếu yêu cầu ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                message = DataAll.Update("PhieuYeuCau", "TrangThai =" + Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu) + " WHERE ID =" + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                if (message.Equals("success"))
                {
                    var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu));
                    if (data.Rows.Count <= 0)
                    {
                        DataAll.InsertAll("HanhTrinh", dataview[gvPhieuYeuCau.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongLenh)
                            + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["TuNgay"]).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["DenNgay"]).ToString("MM/dd/yyyy"));
                        
                    }
                }
                MessageBox.Show(message.Equals("success") ? "Duyệt phiếu yêu cầu thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhieuYeuCau.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn từ chối duyệt phiếu này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("PhieuYeuCau", "TrangThai =" + Convert.ToInt32(TrangThai.PhieuYeuCau_HuyDuyet) + " WHERE ID =" + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                MessageBox.Show(message.Equals("success") ? "Từ chối duyệt phiếu thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }
    }
}
